#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define inf 8e18
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
bitset<3030> suf[3030],f[3030][3];
vector<pair<ll,ll> > vec[60060];
ll t,n,m,a[8080],pri[60060],cnt=0;
bl ck[60060];
struct point{
	ll x,y;
}p[8080];
ll dis(point x,point y){
	return abs(x.x-y.x)+abs(x.y-y.y);
}
struct sub{
	void solve(){
		ll ans=0;
		fr(i,1,n){
			fr(j,i+1,n){
				fr(k,j+1,n){
					ll d1=dis(p[i],p[j]),d2=dis(p[j],p[k]),d3=dis(p[k],p[i]);
					if(ck[d1+d2+d3-min(d1,min(d2,d3))-max(d1,max(d2,d3))]) ans+=a[i]+a[j]+a[k];
				}
			}
		}
		writeln(ans);
	}
}sub;
il void init(ll N){
	fr(i,2,N){
		if(!ck[i]) pri[++cnt]=i;
		fr(j,1,cnt){
			if(i*pri[j]>N) break;
			ck[i*pri[j]]=1;
			if(i%pri[j]==0) break;
		}
	}
	fr(i,1,N) ck[i]=0;
	fr(i,1,cnt) if(pri[i]%20==3||pri[i]<=10) ck[pri[i]]=1;
}
il void clr(){
	fr(i,1,n){
		suf[i].reset();
		fr(j,0,2) f[i][j].reset();
	}
	fr(i,0,2*m) vec[i].clear();
}
int main(){
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	init(60000);
	t=read();
	while(t--){
		n=read();
		m=read();
		fr(i,1,n){
			p[i].x=read();
			p[i].y=read();
		}
		fr(i,1,n) a[i]=read();
		if(n<=100){
			sub.solve();
			clr();
			continue;
		}
		fr(i,1,n){
			fr(j,i+1,n){
				ll d=dis(p[i],p[j]);
				vec[d].push_back(MP(i,j));
			}
		}
		suf[n][n]=1;
		pfr(i,n-1,1){
			suf[i]=suf[i+1];
			suf[i][i]=1;
		}
		fr(i,1,n) f[i][2]=suf[1];
		ll ans=0;
		fr(d,0,2*m){
			fr(i,0,(ll)vec[d].size()-1){
				ll x=vec[d][i].fir,y=vec[d][i].sec;
				f[x][2][y]=f[y][2][x]=0;
				f[x][1][y]=f[y][1][x]=1;
			}
			if(ck[d]){
				fr(i,0,(ll)vec[d].size()-1){
					ll x=vec[d][i].fir,y=vec[d][i].sec;
					ans+=(f[x][1]&suf[y+1]).count();
					ans+=(f[y][1]&(f[x][0]|f[x][2])&suf[x+1]).count();
					ans+=(f[x][0]&f[y][2]).count();
					ans+=(f[x][2]&f[y][0]).count();
				}
			}
			fr(i,0,(ll)vec[d].size()-1){
				ll x=vec[d][i].fir,y=vec[d][i].sec;
				f[x][1][y]=f[y][1][x]=0;
				f[x][0][y]=f[y][0][x]=1;
			}
		}
		writeln(3*ans);
		clr();
	}
}
